
/*******************************************\
|                                           |
| Header file that displays the hard Level  |
| high scores	                            |
|                                           |
| Call highscore_hard () with int score.    |
|                                           |
|                                           |
\*******************************************/

#include <ncurses.h>
#include <stdio.h>
#include <string.h>

FILE *fp;

/* Inserts the new highscore and prints the high score list from ./highscore_list_h.txt */
int hard_score(char name[])
{
        int x, y;               /* column and row */
        char i, buffer[20];     /* Array to read file lines into */

        clear();

        /* Prints header for high score screen. */
        mvprintw(2, (getmaxx(stdscr)/2) - 5, "HIGH SCORES");
        mvprintw(3, (getmaxx(stdscr)/2) - 5, "LEVEL: HARD");
        refresh();

        fp = fopen("./highscore_list_h.txt", "a");      /* Opens highscore list to append. */

        fprintf(fp, "%s\t\t%6d", name, score);          /* Appends new name and score to file. */

        fclose(fp);

        system("./enter_hard.sh");            /* Runs script that organizes highscores by score. */

        x = 1;
        y = 5;

        fp = fopen("./highscore_list_h.txt", "r");      /* Reopens file for reading. */

        while(fgets(buffer, 15, fp) != NULL)    /* Reads and prints the txt file line by line. */
        {
                y++;
                mvprintw(y, x, "%s", buffer);
        }

        fclose(fp);


        mvprintw(23, 1, "Press ENTER to continue.");
        refresh();

        while((i = getch()) != 10) {}          /* Ends when user types 'q' */

        return 0;
}

/* Allows user to enter initials for high score. */
/* Accepts every other input. Solution pending.  */
int highscore_hard()
{
	int initials = 0;
        char name[10];           /* Array to store user initials. */
        clear();
	mvprintw(4, 7, "Your score is %i", score);
        refresh();
        
	while(!initials)
        {
                mvprintw(5, 7, "Please enter your initials ( xxx): ");   /* asks for user name, 3 characters long */
                refresh();
                getstr(name);               /* User enters initials */
                if(name[3] != '\0')
                {
                        mvprintw(7, 7, "Please enter 3 characters.");
                        refresh();
                }
                else
                        initials = 1;
        }

        hard_score(name);  /* Sends new name and score to update the high score list. */


        return 0;
}

